package com.bungarus.cluster;

import com.bungarus.utils.IPAddressUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;

/**
 *
 * Created by tang on 2018/11/14.
 */
public interface Node {
    int PROTOCOL_TCP = 0;
    int PROTOCOL_LONG_POLLING = 1;
    int PROTOCOL_BOTH = 2;
    int STATUS_INITIALIZING = 0;
    int STATUS_RUNNING = 1;
    int STATUS_STOP = 2;

    default String getHostAddress() throws NodeException {
        try {
            //InetAddress localAddress = InetAddress.getLocalHost();
            InetAddress localAddress = IPAddressUtils.getLocalHostLANAddress(); // use correct ip to register node when has multiple net cards
            return localAddress.getHostAddress();
        }
        catch (UnknownHostException e) {
            throw new NodeException(e);
        }
    }

    default String getExtraInfo() {
        return "null";
    }
    /**
     * register node into a zk cluster
     */
    void register2Cluster() throws RegisterNodeException;

    /**
     * get node tag
     */
    String getTag();

    int availableProtocol();

    int getStatus();

    void setStatus(int status) throws Exception;

    /**
     * the tag-port string for client's connection to node.
     * the format of tag-port string likes as below：
     *     (tag1|port1,tag2|port2,tag3|port3)
     *
     * @return tag-port string
     */
    String tagPortString4Connection();

    Optional<NodeInfo> getNodeInfo();
}
